<template>
	<view class="content container">
		<view class="top">
			<view class="left-text">到账银行卡</view>
			<view class="right-area" @click="show = true">
				<text>{{selected || '请选择银行卡'}}</text>
				<image class="right-icon" src="../../static/img/my/arrow_right.png"></image>
			</view>
		</view>
		<view class="main-box">
			<view class="title">提现金额</view>
			<view class="input-flex">
				¥<input class="input" v-model="form.apply_price" placeholder="0.00" placeholder-class="input-placehoder" />
			</view>
			<view class="balance-area">我的余额：¥{{money}}<text class="all-out" @click="form.apply_price = money">全部提现</text></view>
		</view>
		<view class="desc">
			<view>1.提现门槛最低5元，第三方提现手续费为1.5%+2元每笔;</view>
			<view>2.每日提现次数2次，提现3个工作日内到账;</view>
			<view>3.提现需要确保银行卡持有者与账户实名身份证一致;</view>
		</view>
		
		<button class="submit" @click="applyCashOut">提交</button>
		
		<u-action-sheet :actions="list" :closeOnClickOverlay="true" @select="changeBank" :title="title" :show="show"></u-action-sheet>
		<u-toast ref="uToast"></u-toast>
		
		<u-modal :show="showTips" content='您还没有绑定银行卡!' confirmText="前往绑定" @confirm="go('/pages/bankcard/bindcard')"></u-modal>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				enable: true,
				money: '0.00',
				list: [],
				title: '',
				show: false,
				selected: null,
				form: {
					bank_id: 0,
					apply_price: null
				},
				showTips: false,
			}
		},
		created() {
			this.loadMoney()
		},
		methods: {
			loadMoney() {
				Promise.all([
					this.$http.get('user/getUserInfo'),
					this.$http.get('user/getUserBankList')
				]).then(result => {
					this.money = result[0].data.money
					this.list = result[1].data.list.map(({id, bank_number, bank_info}) => {
						return {
							id,
							name: (bank_info || '') + '(' + bank_number.substr(0, 5) +  '****' + bank_number.substr(-5) + ')'
						}
					})
					
					if (this.list.length == 0) {
						this.showTips = true
					}
					
				})
			},
			changeBank({id, name}) {
				this.form.bank_id = id
				this.selected = name
				this.show = false
			},
			applyCashOut() {
				if (!this.enable) return false
				this.enable = false
				this.$http.post('user/applyCashOut', this.form).then(res => {
					this.$refs.uToast.show({
						message: res.msg, 
						complete: () => {
							this.enable = true
							
							if (res.code == 1) {
								this.form = {}
								this.loadMoney()
							}
						}
					})
					
				}).catch(() => {
					this.enable = false
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.content.container {
	height: 100%;
	background: #f7f7f7;
	
	.top {
		padding: 40rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		
		.left-text {
			color: #888;
			font-size: 28rpx;
		}
		
		.right-area {
			font-size: 28rpx;
			color: #222;
			font-weight: 600;
			display: flex;
			align-items: center;
			.right-icon {
				margin-left: 20rpx;
				opacity: 0.35;
				height: 32rpx;
				width: 32rpx;
			}
		}
	}
	
	.main-box {
		background: #fff;
		padding: 40rpx;
		
		.title {
			font-size: 28rpx;
			font-weight: 500;
			color: #222;
		}
		
		.input-flex {
			margin-top: 20rpx;
			display: flex;
			align-items: center;
			font-size: 30rpx;
			color: #222;
			padding-bottom: 12rpx;
			border-bottom: solid 1px #f0f0f0;
			
			.input {
				margin-left: 20rpx;
				border: 0;
				flex: 1;
				font-size: 48rpx;
			}
			
			.input-placeholder {
				color: #ccc;
			}
		}
	
		.balance-area {
			margin-top: 20rpx;
			font-size: 28rpx;
			display: flex;
			align-items: center;
			line-height: 1;
			color: #888;
			
			.all-out {
				margin-left: 20rpx;
				color: #0169fc;
			}
		}
	}

	.desc {
		margin: 40rpx;
		font-size: 28rpx;
		color: #aaa;
		line-height: 1.5;
	}
	
	.submit {
		margin-top: 80rpx;
		color: #fff;
		background: #006afa;
		border-radius: 100px;
		height: 80rpx;
		font-size: 28rpx;
		max-width: 560rpx;
	}
}
</style>
